# -*- coding: utf-8 -*-

import gtk
import sys
import os
from gtkmvc import View

class BaseView(View):
    
    GLADE_FILE = os.path.join(sys.path[0], 'pyvr.glade')

    def __init__(self, ctrl, parent_view=None, auto_register=True):
        View.__init__(self, ctrl, self.GLADE_FILE, self.TOP_WIDGET, parent_view, auto_register)



class PyvrView (BaseView):
    """This handles only the graphical representation of the
    application. The widgets set is loaded from glade file"""
    
    TOP_WIDGET = "w_main"

    """ liste des checkbox de langues """
    ckb_lang = []

    def set_title(self, title):
        self['w_main'].set_title(title)
        return

    def set_file_list(self, files):
        """ met a jour la liste deroulante de selection de film """
        self._fill_select('cb_files', files)
        return

    def select_file(self, file):
        """ selectionne le fichier dans le comdo des films """
        self._combo_select('cb_files', file)

    def select_next_file(self):
        """ selectione le fichier suivant dans le combo des films """
        id = self['cb_files'].get_active()
        id += 1
        id = self['cb_files'].set_active(id)
        
    """ ajoute les labels de langues et les cases à cocher """
    def add_lang(self, langs, callback):
        ck_label = {'audio' : 'audio', 'sub' : 'sous-titre'}
        for lang in langs:
            hbox = gtk.HBox(False, 15)
            l = gtk.Label("%s : " % lang)
            l.set_alignment(xalign=0, yalign=0.5) 
            self['vbox_l_lang'].add(l)
            l.show()
            
            for type in ck_label:
                ckb = gtk.CheckButton(ck_label[type])
                ckb.set_name('%s_%s' % (lang, type))
                hbox.pack_start(ckb, False)
                ckb.connect("toggled", callback, {'lang' : lang, 'type' : type})
                self.ckb_lang.append(ckb)
        
            self['vbox_ck_lang'].add(hbox)

        self['vbox_ck_lang'].show_all()
        return

    def set_quality_list(self, qualities):
        self._fill_select('cb_quality', qualities)
        return

    def set_video_codec_list(self, codec):
        self._fill_select('cb_video_codec', codec)
        return

    def set_audio_codec_list(self, codec):
        self._fill_select('cb_audio_codec', codec)
        return

    def set_nfo_presence(self, bool):
        """ met à jour le label qui indique si un nfo a été trouvé """
        
        l_nfo = "trouvé" if(bool) else "aucun"
            
        self['ck_nfo'].set_active(not bool)
        self['ck_nfo'].set_sensitive(not bool)    
        self['l_nfo_found'].set_text(l_nfo)
        return

    def _fill_select(self, select, t):
        """ remplit un combobox existant """
        model = self[select].get_model()
        model.clear()
        
        for item in t:
            model.append([item])
        
        self[select].set_active(0) # selectione la première entrée
        return

    def _combo_select(self, cb, value):
        """ selectionne dans un combo la valeur passe en parametre"""
        m = self[cb].get_model()
        id = 0
        for item in m:
            if item[0] == value: 
                self[cb].set_active(id)
                return

            id += 1

        self[cb].set_active(0)
        return

    """ met a jour la zone de description technique du film """
    def set_film_specs(self, vcodec, vres, vqual, acodec, atracks, alang, slang):
        
        # video
        self['l_video_codec'].set_text(vcodec)
        self._combo_select('cb_video_codec', vcodec)
        self['l_video_resolution'].set_text(vres)
        self['l_video_quality'].set_text(vqual)
        self._combo_select('cb_quality', vqual)
        
        # audio
        self['l_audio_codec'].set_text(acodec)
        self._combo_select('cb_audio_codec', acodec)
        
        self['l_audio_track'].set_text(atracks)
        self['l_audio_languages'].set_text((',').join(alang))
        
        # check langage specs
        d_lang = {'audio' : alang, 'sub' : slang}
        for ckb in self.ckb_lang: # chaque checkbox
            name = ckb.get_name()
            (lang, type) = name.split('_')
            ckb.set_active(d_lang[type].count(lang))
        return

    pass # end of class

class PyvrChooseFolderView(BaseView):
    TOP_WIDGET = 'w_open_folder' # nom du widget danls fichier glade
    
    
    def run(self):
        w = self.get_top_widget()
        w.run()
        w.destroy()
        return
    pass